package com.ruoyix.ai.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyix.common.annotation.RepeatSubmit;
import com.ruoyix.common.annotation.Log;
import com.ruoyix.common.core.controller.BaseController;
import com.ruoyix.common.core.domain.PageQuery;
import com.ruoyix.common.core.domain.R;
import com.ruoyix.common.core.validate.AddGroup;
import com.ruoyix.common.core.validate.EditGroup;
import com.ruoyix.common.core.validate.QueryGroup;
import com.ruoyix.common.enums.BusinessType;
import com.ruoyix.common.utils.poi.ExcelUtil;
import com.ruoyix.ai.domain.vo.AiChatModelVo;
import com.ruoyix.ai.domain.bo.AiChatModelBo;
import com.ruoyix.ai.service.IAiChatModelService;
import com.ruoyix.common.core.page.TableDataInfo;

/**
 * AI 聊天模型
 *
 * @author ruoyi
 * @date 2024-09-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/aiChatModel")
public class AiChatModelController extends BaseController {

    private final IAiChatModelService iAiChatModelService;

    /**
     * 查询AI 聊天模型列表
     */
    @SaCheckPermission("ai:aiChatModel:list")
    @GetMapping("/list")
    public TableDataInfo<AiChatModelVo> list(AiChatModelBo bo, PageQuery pageQuery) {
        return iAiChatModelService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出AI 聊天模型列表
     */
    @SaCheckPermission("ai:aiChatModel:export")
    @Log(title = "AI 聊天模型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AiChatModelBo bo, HttpServletResponse response) {
        List<AiChatModelVo> list = iAiChatModelService.queryList(bo);
        ExcelUtil.exportExcel(list, "AI 聊天模型", AiChatModelVo.class, response);
    }

    /**
     * 获取AI 聊天模型详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("ai:aiChatModel:query")
    @GetMapping("/{id}")
    public R<AiChatModelVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iAiChatModelService.queryById(id));
    }

    /**
     * 新增AI 聊天模型
     */
    @SaCheckPermission("ai:aiChatModel:add")
    @Log(title = "AI 聊天模型", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AiChatModelBo bo) {
        return toAjax(iAiChatModelService.insertByBo(bo));
    }

    /**
     * 修改AI 聊天模型
     */
    @SaCheckPermission("ai:aiChatModel:edit")
    @Log(title = "AI 聊天模型", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AiChatModelBo bo) {
        return toAjax(iAiChatModelService.updateByBo(bo));
    }

    /**
     * 删除AI 聊天模型
     *
     * @param ids 主键串
     */
    @SaCheckPermission("ai:aiChatModel:remove")
    @Log(title = "AI 聊天模型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iAiChatModelService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
